package com.huawei.esdk.uc.sametime.toolbar.actions;

import org.apache.log4j.Logger;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;

import com.huawei.esdk.uc.jna.Constants;
import com.huawei.esdk.uc.jna.Constants.PhoneJointStatusType;
import com.huawei.esdk.uc.jna.Constants.PhoneJointType;
import com.huawei.esdk.uc.sametime.ConstantsDefines;
import com.huawei.esdk.uc.sametime.services.UCSettingsService;
import com.huawei.esdk.uc.sametime.toolbar.ToolbarPhoneCtlCascadeMenuCreator;
import com.huawei.esdk.uc.sametime.uccallbackimpl.PhoneJoinCallBackInterface;
import com.huawei.esdk.uc.sametime.uccallbackimpl.PhoneJointEventCBImpl;
import com.huawei.esdk.uc.sametime.uccallbackimpl.UCLoginCallBackImpl;
import com.huawei.esdk.uc.sametime.uccallbackimpl.UCLoginCallBackInterface;
import com.huawei.esdk.uc.sametime.util.GlobalConfiguration;
import com.huawei.esdk.uc.sametime.util.ImageFactory;
import com.ibm.collaboration.realtime.imhub.actions.BuddyListAction;

/**
 * 呼出设备选择控制类
 * @author y00175154
 * 
 */
public class EsdkPhoneCtlDropDownAction extends BuddyListAction implements
        UCLoginCallBackInterface, PhoneJoinCallBackInterface
{

    private static Logger logger = Logger
            .getLogger(EsdkPhoneCtlDropDownAction.class);

    // 控件对应的菜单控制器
    private ToolbarPhoneCtlCascadeMenuCreator menuCreator;

    private boolean isRefreshed;

    // pc模式的图片容器
    private ImageDescriptor pcModeImageDesc;

    // ip话机模式的图片容器
    private ImageDescriptor phoneModeImageDesc;

    // pc模式被禁用的图片容器
    private ImageDescriptor pcModeDisabledImageDesc;

    // ip话机被禁用的图片容器
    private ImageDescriptor phoneModeDisabledImageDesc;

    private String currentMode;

    public EsdkPhoneCtlDropDownAction()
    {
        super(null);

        menuCreator = new ToolbarPhoneCtlCascadeMenuCreator(this);
        setMenuCreator(menuCreator);
        UCLoginCallBackImpl.getInstance().addObserver(this);
        PhoneJointEventCBImpl.getInstance().addObserver(this);

        pcModeImageDesc = ImageDescriptor.createFromImage(ImageFactory
                .loadImage(Display.getCurrent(), ImageFactory.PC_MODE));
        pcModeDisabledImageDesc = ImageDescriptor
                .createFromImage(ImageFactory.loadImage(Display.getCurrent(),
                        ImageFactory.PC_MODE_DISABLED));

        phoneModeImageDesc = ImageDescriptor.createFromImage(ImageFactory
                .loadImage(Display.getCurrent(), ImageFactory.PHONE_MODE));
        phoneModeDisabledImageDesc = ImageDescriptor
                .createFromImage(ImageFactory.loadImage(Display.getCurrent(),
                        ImageFactory.PHONE_MODE_DISABLED));

        refreshActionImage(Constants.PhoneJointType.PC_Device);
    }

    @Override
    public void run()
    {
        Rectangle rect = getToolItemBounds();
        Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
                .getShell();
        Menu menu = menuCreator.getMenu(shell);
        if (null != rect)
        {
            menu.setLocation(rect.x, rect.y + rect.height);
        }
        menu.setVisible(true);

    }

    /**
     * 
     * @param currentModel 当前呼叫设备的选择模式,pc (0)或者ipphone(1)
     */
    private void refreshActionImage(int currentCallDev)
    {

        logger.info("currentCallDev = " + currentCallDev);

        if (Constants.PhoneJointType.PC_Device == currentCallDev)
        {
            setImageDescriptor(pcModeImageDesc);
            setDisabledImageDescriptor(pcModeDisabledImageDesc);
        }
        else if (Constants.PhoneJointType.IPPhone_Device == currentCallDev)
        {
            setImageDescriptor(phoneModeImageDesc);
            setDisabledImageDescriptor(phoneModeDisabledImageDesc);
        }
        else
        {
            setImageDescriptor(pcModeImageDesc);
            setDisabledImageDescriptor(pcModeDisabledImageDesc);
        }
    }

    @Override
    public boolean isEnabled()
    {
        // 这里判断插件是否登录UC,只有登录后才能返回true
        boolean isUCLogin = GlobalConfiguration.getInstance().isUCLogin();
        boolean isSametimeLogin = GlobalConfiguration.getInstance()
                .isSamtimeLogin();

        if (isSametimeLogin && isUCLogin)
        {
            // 如果没有ip phone号码,则直接禁用选择呼出设备的功能
            if (!isRefreshed)
            {
                Display.getDefault().syncExec(new Runnable()
                {
                    @Override
                    public void run()
                    {
                         currentMode = UCSettingsService.getPhoneJoinDev();
                        
                        if (ConstantsDefines.PC_MODE.equals(currentMode))
                        {
                            refreshActionImage(Constants.PhoneJointType.PC_Device);
                        }
                        else if (ConstantsDefines.IP_PHONE_MODE
                                .equals(currentMode))
                        {
                            refreshActionImage(Constants.PhoneJointType.IPPhone_Device);
                        }
                        else
                        {
                            logger.error("get ip phone error or no right !");
                        }
                    }
                });
                isRefreshed = true;
            }
            if (null != currentMode
                    && (ConstantsDefines.PC_MODE.equals(currentMode) || ConstantsDefines.IP_PHONE_MODE
                            .equals(currentMode)))
            {
                return true;
            }
        }

        return false;
    }

    @Override
    public void handleLogin(int state, String reason)
    {
        if (Constants.SignInState.Client_SignedIn != state)
        {
            // 如果能监听到未登录成功,则说明先前刷新过一次了.则将被刷新的标记位重置
            isRefreshed = false;
        }
    }

    @Override
    public void handlePhoneJoin(int state)
    {
        logger.info("handlePhoneJoin | state = " + state);

        int currDeviceType = -1;

        if (PhoneJointStatusType.STATUS_START_SUCC == state
                || PhoneJointStatusType.STATUS_STOP_FAILED == state)
        {
            currDeviceType = PhoneJointType.IPPhone_Device;
        }
        else if (PhoneJointStatusType.STATUS_START_FAILED == state
                || PhoneJointStatusType.STATUS_STOP_SUCC == state)
        {
            currDeviceType = PhoneJointType.PC_Device;
        }
        else
        {
            logger.info("handlePhoneJoin | state = " + state);
        }

        if (-1 != currDeviceType)
        {
            GlobalConfiguration.getInstance().setCallDevice(currDeviceType);
            refreshActionImage(currDeviceType);
        }
    }

}
